package com.jiangjiang.eshop.comment.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Sets;
import com.jiangjiang.eshop.comment.constant.CommentApproved;
import com.jiangjiang.eshop.comment.constant.CommentStatus;
import com.jiangjiang.eshop.comment.convert.CommentInfoConvert;
import com.jiangjiang.eshop.comment.domain.CommentInfoEntity;
import com.jiangjiang.eshop.comment.domain.CommentPictureEntity;
import com.jiangjiang.eshop.comment.dto.CommentDetailDTO;
import com.jiangjiang.eshop.comment.dto.CommentInfoDTO;
import com.jiangjiang.eshop.comment.dto.CommentPageQueryDTO;
import com.jiangjiang.eshop.comment.mapper.CommentInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName CommentInfoService
 * @Description TODO
 * @Author jhw
 * @Date 2021/5/17 21:36
 * @Version 1.0
 **/
@Slf4j
@Service
public class CommentInfoService extends ServiceImpl<CommentInfoMapper, CommentInfoEntity> {
    @Autowired
    private CommentInfoConvert commentConvert;
    @Autowired
    private CommentPictureService commentPictureService;

    /**
     *
     * @param commentInfoDTO 评论新增DTO
     * @return 评论新增id
     */
    public Long saveCommentInfo(CommentInfoDTO commentInfoDTO) {
        CommentInfoEntity entity = commentConvert.convertInfoDTO2Entity(commentInfoDTO);
        this.save(entity);
        return entity.getId();
    }

    /**
     * 评论数据分页查询
     * @param dto 分页查询DTO
     * @return 分页查询结果
     */
    public Page<CommentInfoDTO> queryForPage(CommentPageQueryDTO dto) {
        Page<CommentInfoEntity> page = new Page<>(dto.getPageNo(), dto.getPageSize(), true);
        LambdaQueryWrapper<CommentInfoEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(dto.getShowPictures() != null, CommentInfoEntity::getShowPictures, dto.getShowPictures())
                .eq(dto.getDefaultComment() != null, CommentInfoEntity::getDefaultComment, dto.getDefaultComment())
                .eq(dto.getCommentStatus() != null, CommentInfoEntity::getCommentStatus, dto.getCommentStatus())
                .eq(dto.getCommentType() != null, CommentInfoEntity::getCommentType, dto.getCommentType())
                .between(Objects.nonNull(dto.getStartTime()) && Objects.nonNull(dto.getEndTime()),
                        CommentInfoEntity::getGmtCreate, dto.getStartTime(), dto.getEndTime());

        Page<CommentInfoDTO> result = commentConvert.convertPageEntity2DTO(this.getBaseMapper().selectPage(page, wrapper));
        // 补充评论图片数据
        handlerCommentPicture(result);

        return result;
    }

    /**
     * 处理评论图片
     * @param result
     */
    private void handlerCommentPicture(Page<CommentInfoDTO> page) {
        Set<Long> commentInfoIds = page.getRecords().stream().map(CommentInfoDTO::getId).collect(Collectors.toSet());
        Map<Long, List<String>> pictureMap = commentPictureService.queryPicturesByCommentInfoIds(commentInfoIds)
                .stream()
                .collect(Collectors.groupingBy(
                        CommentPictureEntity::getId,
                        Collectors.mapping(CommentPictureEntity::getCommentPicturePath, Collectors.toList()))
                );

        page.getRecords().forEach(dto -> dto.setPictureUrls(pictureMap.get(dto.getId())));
    }

    /**
     * 根据id查询评论详情
     * @param id 评论id
     * @return 查询结果
     */
    public CommentDetailDTO queryById(Long id) {
        LambdaQueryWrapper<CommentInfoEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CommentInfoEntity::getId,id);
        CommentInfoEntity entity = this.getOne(wrapper);

        // 查询评论图片
        List<String> pictureUrls = commentPictureService.queryPicturesByCommentInfoIds(Sets.newHashSet(entity.getId()))
                .stream()
                .map(CommentPictureEntity::getCommentPicturePath)
                .collect(Collectors.toList());

        return commentConvert.convertInfo2DetailDTO(entity,pictureUrls);
    }

    /**
     * 评论审核
     * @param id 评论id
     * @param approved 审核状态
     * @return 处理结果
     */
    public Boolean approveComment(Long id, Integer approved) {
        LambdaUpdateWrapper<CommentInfoEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(CommentInfoEntity::getId, id)
                .set(CommentInfoEntity::getCommentStatus, CommentApproved.YES.equals(approved) ?
                        CommentStatus.APPROVED : CommentStatus.APPROVE_REJECTED);

        return this.update(null,wrapper);
    }
}
